

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;




import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextArea;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class WaresAddFrame extends JFrame {

	private JPanel contentPane;
	private JTextField categoryField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					WaresAddFrame frame = new WaresAddFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public WaresAddFrame() {
		setTitle("商品类别添加");
		//设置窗体位置与大小
		setBounds(100, 100, 450, 300);
		//设置关闭操作的事件
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//固定窗口大小
		setSize(600,400);
		//设置窗口的位置为居中显示
		setLocationRelativeTo(null);
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		GridBagLayout gbl_panel = new GridBagLayout();
		panel.setLayout(gbl_panel);
		
		JLabel categoryLabel = new JLabel("商品类别名称");
		GridBagConstraints gbc_categoryLabel = new GridBagConstraints();
		gbc_categoryLabel.insets = new Insets(5, 10, 5, 5);
		gbc_categoryLabel.gridx = 0;
		gbc_categoryLabel.gridy = 1;
		panel.add(categoryLabel,gbc_categoryLabel );
		
		categoryField = new JTextField();
		GridBagConstraints gbc_categoryField = new GridBagConstraints();
		gbc_categoryField.insets = new Insets(5, 5, 5, 10);
		//填充拉升文本框
		gbc_categoryField.fill=GridBagConstraints.HORIZONTAL;
		gbc_categoryField.gridx = 1;
		gbc_categoryField.gridy = 1;
		gbc_categoryField.weightx = 1;
		panel.add(categoryField, gbc_categoryField);
		
		
		JLabel describeLabel = new JLabel("商品类别描述");
		GridBagConstraints gbc_describeLabel = new GridBagConstraints();
		gbc_describeLabel.insets = new Insets(5, 10, 5, 5);
		gbc_describeLabel.gridx = 0;
		gbc_describeLabel.gridy = 2;
		panel.add(describeLabel, gbc_describeLabel);
		
		JTextArea describeArea = new JTextArea();
		describeArea.setRows(5);
		GridBagConstraints gbc_describeArea = new GridBagConstraints();
		gbc_describeArea.insets = new Insets(5, 5, 5, 10);
		gbc_describeArea.fill=GridBagConstraints.BOTH;
		gbc_describeArea.gridx = 1;
		gbc_describeArea.gridy = 2;
		gbc_describeArea.weightx = 1;
		panel.add(describeArea, gbc_describeArea);
		
		//添加按钮
		JButton btnNewButton = new JButton("添加");
		btnNewButton.setIcon(new ImageIcon("./images/add.png"));
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.insets = new Insets(5, 5, 10, 5);
		gbc_btnNewButton.gridx = 1;
		gbc_btnNewButton.gridy = 3;
		gbc_btnNewButton.anchor = GridBagConstraints.WEST;
		panel.add(btnNewButton, gbc_btnNewButton);
		//类别添加事件
		btnNewButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				String productTypeName=categoryField.getText();
				String productTypeDesc=describeArea.getText();
				//调用添加分类数据的方法
				ProductTypeDao productTypeDao=new ProductTypeDao();
				boolean flag=false;
				try {
					 flag =productTypeDao.addProductType(productTypeName, productTypeDesc);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				if(flag) {
					setVisible(false);
				}else {
					JOptionPane.showMessageDialog(null, "商品类别添加失败");
				}
			}
		});
			
		
		
		//重置按钮
		JButton resetButton = new JButton("重置");
		resetButton.setIcon(new ImageIcon("./images/reset.png"));
		 GridBagConstraints gbc_resetButton = new GridBagConstraints();
		 gbc_resetButton.gridx = 1; // 1 列
		 gbc_resetButton.gridy = 3; // 3 ⾏
		 gbc_resetButton.weightx = 1; // ⽔平权重
		 gbc_resetButton.insets = new Insets(5, 5, 10, 10);
		 panel.add(resetButton, gbc_resetButton);
		 
		 //重置按钮的点击事件
		 resetButton.addActionListener(new ActionListener() {
				
			 @Override
				public void actionPerformed(ActionEvent e) {
					//清空用户名和密码
				 categoryField.setText("");
				 describeArea.setText("");
				 
				 
			 }
		 });
	}

}
